package myjpush.administrator.example.com.myhomework;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import myjpush.administrator.example.com.myhomework.fragment.Br_Friend;
import myjpush.administrator.example.com.myhomework.fragment.Br_life;
import myjpush.administrator.example.com.myhomework.fragment.Br_message;
import myjpush.administrator.example.com.myhomework.fragment.Br_person;
import myjpush.administrator.example.com.myhomework.fragment.Br_school;

public class MainActivity extends Activity {
    private Br_Friend br_friend = null;
    private Br_life br_life = null;
    private Br_message br_message = null;
    private Br_person br_person = null;
    private Br_school br_school = null;
    private FragmentManager fm = null;
    private RadioGroup rgBottomBar;
    private RadioButton rbItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getFragmentManager();
        initViews();
    }
    private void initViews(){
        rgBottomBar = (RadioGroup) findViewById(R.id.rg_bottom_bar);
        rgBottomBar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int i) {
                FragmentTransaction ft = fm.beginTransaction();
                hideAllFraments(ft);
                switch (i) {
                    case R.id.br_friend:
                        if (null == br_friend) {
                            br_friend = new Br_Friend();
                            ft.add(R.id.fl_main, br_friend);
                        } else {
                            ft.show(br_friend);
                        }
                        break;
                    case R.id.br_life:
                        if (null == br_life) {
                            br_life = new Br_life();
                            ft.add(R.id.fl_main, br_life);
                        } else {
                            ft.show(br_life);
                        }
                        break;
                    case R.id.br_message:
                        if (null == br_message) {
                            br_message = new Br_message();
                            ft.add(R.id.fl_main, br_message);
                        } else {
                            ft.show(br_message);
                        }
                        break;
                    case R.id.br_person:
                        if (null == br_person) {
                            br_person = new Br_person();
                            ft.add(R.id.fl_main, br_person);
                        } else {
                            ft.show(br_person);
                        }
                        break;
                    case R.id.br_school:
                        if (null == br_school) {
                            br_school = new Br_school();
                            ft.add(R.id.fl_main,br_school);
                        } else {
                          ft.show(br_school);
                        }
                }
                ft.commit();
            }
        });
        rbItem = (RadioButton) findViewById(R.id.br_friend);
        rbItem.setChecked(true);
    }
    private void hideAllFraments (FragmentTransaction ft) {
        if (null != br_friend){ft.hide(br_friend);}
        if (null != br_life){ft.hide(br_life);}
        if (null != br_message){ft.hide(br_message);}
        if (null != br_person){ft.hide(br_person);}
        if (null != br_school){ft.hide(br_school);}
    }
}

